package com.tencent.qcloud.timchat.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bxd.baodan.R;
import com.tencent.qcloud.timchat.model.Conversation;
import com.tencent.qcloud.timchat.utils.TimeUtil;
import com.tencent.qcloud.ui.CircleImageView;

import java.util.List;

/**
 * 会话界面adapter
 */
public class ConversationAdapter extends ArrayAdapter<Conversation> {
    private Context context;
    private int resourceId;
    private View view;
    private ViewHolder viewHolder;
    public boolean hasUnRead;

    /**
     * Constructor
     *
     * @param context  The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects  The objects to represent in the ListView.
     */
    public ConversationAdapter(Context context, int resource, List<Conversation> objects) {
        super(context, resource, objects);
        resourceId = resource;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null) {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) view.findViewById(R.id.name);
            viewHolder.avatar = (CircleImageView) view.findViewById(R.id.avatar);
            viewHolder.lastMessage = (TextView) view.findViewById(R.id.last_message);
            viewHolder.time = (TextView) view.findViewById(R.id.message_time);
            viewHolder.unread = (TextView) view.findViewById(R.id.unread_num);
            viewHolder.verify_icon = (ImageView) view.findViewById(R.id.verify_icon);
            viewHolder.verify_level = (TextView) view.findViewById(R.id.verify_level);
            view.setTag(viewHolder);
        }
        final Conversation data = getItem(position);

// TODO: 17/9/20 修改


        viewHolder.tvName.setText((data.getWenwenName() != null && !"".equals(data.getWenwenName())) ? data.getWenwenName() : data.getName());
        Glide.with(getContext()).load(data.getMyAvatar()).error(R.drawable.empty_avatar).into(viewHolder.avatar);
        viewHolder.lastMessage.setText(data.getLastMessageSummary());
        viewHolder.time.setText(TimeUtil.getTimeStr(data.getLastMessageTime()));

        long unRead = data.getUnreadNum();
        if (unRead <= 0) {
            viewHolder.unread.setVisibility(View.INVISIBLE);
        } else {
            hasUnRead = true;
            viewHolder.unread.setVisibility(View.VISIBLE);
            String unReadStr = String.valueOf(unRead);
            if (unRead < 10) {
                viewHolder.unread.setBackground(getContext().getResources().getDrawable(R.drawable.point1));
            } else {
                viewHolder.unread.setBackground(getContext().getResources().getDrawable(R.drawable.point2));
                if (unRead > 99) {
                    unReadStr = getContext().getResources().getString(R.string.time_more);
                }
            }
            viewHolder.unread.setText(unReadStr);
        }

        String verify = "";
        int level = data.getAgentLevel();
        int verColor = R.color.white;
        int drawableLeft = R.color.transparent;


        switch (level) {
            case 0:
                break;
            case 4:
                verify = "保险管家";
                verColor = R.color.guanja;
                drawableLeft = R.drawable.round_corner_yellow;
                break;
            case 2:
                verify = "保险精英";
                verColor = R.color.jingying;
                drawableLeft = R.drawable.round_corner_blue_jingying;
                break;
            case 1:
                verify = "保险达人";
                verColor = R.color.daren;
                drawableLeft = R.drawable.round_corner_green;
                break;
            case 3:
                verify = "保险专家";
                verColor = R.color.zhuanjia;
                drawableLeft = R.drawable.round_corner_red_zhuanjia;
                break;
            default:
                break;
        }

        viewHolder.verify_level.setBackground(context.getResources().getDrawable(drawableLeft));


        viewHolder.verify_level.setText(verify);
        viewHolder.verify_level.setTextColor(context.getResources().getColor(verColor));
        viewHolder.verify_icon.setVisibility(data.isVerify() ? View.VISIBLE : View.GONE);


        return view;
    }

    public class ViewHolder {
        public TextView tvName;
        public CircleImageView avatar;
        public TextView lastMessage;
        public TextView time;
        public TextView unread;
        public ImageView verify_icon;
        public TextView verify_level;


    }
}
